package domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "player")
public class Player {
	
	@XmlElement(name = "firstname")
	private String firstname;
	@XmlElement(name = "lastname")
	private String lastname;
	@XmlElement(name = "whiteBlackBalance")
	private int whiteBlackBalance;
	@XmlElement(name = "amountOfPoints")
	private int amountOfPoints;
	@XmlElement(name = "amountOfGamesPlayed")
	private int amountOfGamesPlayed;
	
	//TODO Part if each Player knows its games
	private List<Game> games = new ArrayList<Game>();

	public Player(String firstname, String lastname) {
		setFirstname(firstname);
		setLastname(lastname);
	}
	
	protected boolean isSamePlayer(Player targetPlayer)
	{
		boolean isSame = false;
		if(targetPlayer.getFirstname().equals(firstname) && targetPlayer.getLastname().equals(lastname))
			isSame = true;
		return isSame;
	}
	
	protected int howManyGamesAgoLastPlayedAgainstPlayer(Player targetPlayer)
	{
		Collections.sort(this.games, new Comparator<Game>(){
			@Override
			public int compare(Game g1, Game g2) {
				// Natural ordening with dates <=> less recent to most recent
				// The "-" reverses the natural ordening
				return - (g1.getDate().compareTo(g2.getDate()));
			}
		});
		
		int returnValueGamesAgo = Integer.MIN_VALUE;
		// TODO 
		for(int gamesAgo = 1; gamesAgo -1 < this.games.size(); gamesAgo++)
		{
			if(this.games.get(gamesAgo - 1).isGameAgainstPlayer(targetPlayer))
			{
				returnValueGamesAgo = gamesAgo;
				break;
			}
		}
		return returnValueGamesAgo;
	}
	
	protected int compareWhiteBlackBalance(Player adversary)
	{
		int comparisonWBBalance = (whiteBlackBalance - adversary.whiteBlackBalance);
		return comparisonWBBalance;
	}
	
	protected boolean isNewPlayer()
	{
		return games.isEmpty();
	}
	
	public String toString()
	{
		return firstname + " " + lastname;
	}
	
	public String[] toArray()
	{
		String[] array = new String[2];
		array[0] = firstname;
		array[1] = lastname;
		return array;
	}
	
	public String[] toRankingTable()
	{
		String[] formattedRankedPlayer = new String[3];
		formattedRankedPlayer[0] = firstname + " " + lastname;
		formattedRankedPlayer[1] = String.valueOf(amountOfPoints);
		formattedRankedPlayer[2] = String.valueOf(amountOfGamesPlayed);
		return formattedRankedPlayer;
	}
	
	// GETTERS
	public String getFirstname() {
		return firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public int getWhiteBlackBalance() {
		return whiteBlackBalance;
	}
	
	public int getAmountOfPoints() {
		return amountOfPoints;
	}

	public int getAmountOfGamesPlayed() {
		return amountOfGamesPlayed;
	}
	
	public List<Game> getGames() {
		return games;
	}

	// SETTERS
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public void setWhiteBlackBalance(int whiteBlackBalance) {
		this.whiteBlackBalance = whiteBlackBalance;
	}

	public void setAmountOfPoints(int amountOfPoints) {
		this.amountOfPoints = amountOfPoints;
	}

	public void setAmountOfGamesPlayed(int amountOfGamesPlayed) {
		this.amountOfGamesPlayed = amountOfGamesPlayed;
	}
	
	public void setDummyGames(List<Game> dummyGames)
	{
		this.games = dummyGames;
	}
}